package com.fjt.book_trade.controller;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.fjt.book_trade.pojo.Order;
import com.fjt.book_trade.pojo.Product;
import com.fjt.book_trade.pojo.User;
import com.fjt.book_trade.service.OrderService;
import com.fjt.book_trade.service.ProductService;
import com.fjt.book_trade.service.UserService;

@Controller
public class ProductController {
	private ProductService productService;
	private OrderService orderService;
	private UserService userService;
	public UserService getUserService() {
		return userService;
	}
	public void setUserService(UserService userService) {
		this.userService = userService;
	}
	public ProductService getProductService() {
		return productService;
	}
	@Resource(name="productservice")
	public void setProductService(ProductService productService) {
		this.productService = productService;
	}
	public OrderService getOrderService() {
		return orderService;
	}
	@Resource(name="orderservice")
	public void setOrderService(OrderService orderService) {
		this.orderService = orderService;
	}
	
	@RequestMapping("/product/productlist") 
    @ResponseBody
    public ModelAndView getproductlist(HttpServletRequest request){
    	Map<String,Object> modelMap=new HashMap<String,Object>();
    	ModelAndView modelView=new ModelAndView(); 
		modelMap=productService.getProductlist();
    	modelView.addAllObjects(modelMap);  
    	System.out.println("/product/productlist");
    	return modelView;
    	
    }
	
	@RequestMapping("/product/myproductlist") 
    @ResponseBody
    public ModelAndView getmyproductlist(HttpServletRequest request){
    	Map<String,Object> modelMap=new HashMap<String,Object>();
    	ModelAndView modelView=new ModelAndView(); 
    	HttpSession session=request.getSession();
    	String username=(String)session.getAttribute("username");
    	System.out.println(username);
//    	order.setAddress(userService.getUserDao().findUserInfoByName(username).getAddress());
    	if(username!=null){
    		modelMap=productService.getMyProductlist(username);
        	modelMap.put("result", "success");
    		modelView.addAllObjects(modelMap);
        	System.out.println("/product/myproductlist");
        	return modelView;
    	}else{
    		modelMap.put("result", "fail");
    		modelView.addAllObjects(modelMap);
    		System.out.println("/product/myproductlist no session");
    		return modelView;
    	}
    	
    }
    
    @RequestMapping("/product/productinfo") 
    @ResponseBody
    public ModelAndView getProductInfo(@RequestBody Product product,
    		HttpServletRequest request){  
    	Map<String,Object> modelMap=new HashMap<String,Object>();
    	ModelAndView modelView=new ModelAndView();
		modelMap=productService.getProductInfo(product.getProduct_id());
    	modelView.addAllObjects(modelMap);  
    	System.out.println("/product/productinfo");
    	return modelView;
    	
    }
    
    @RequestMapping(value="/product/addproduct",produces="application/json") 
    @ResponseBody
    public ModelAndView addProduct(@RequestBody Product product,HttpServletRequest request,HttpServletResponse response){  
    	Map<String,String> modelMap=new HashMap<String,String>();
    	System.out.println(request.getContentType());
    	ModelAndView modelView=new ModelAndView(); 
    	HttpSession session=request.getSession();
    	String username=(String)session.getAttribute("username");
    	response.setContentType("application/json");
    	System.out.println(response.getContentType());
    	System.out.println(product.getProduct_name());
    	if(username!=null){
    		product.setSeller_name(username);
    		modelMap=productService.addProduct(product);
        	modelView.addAllObjects(modelMap); 
        	System.out.println("/product/addproduct");
        	return modelView;
    	}else{
    		modelMap.put("result", "fail");
    		modelView.addAllObjects(modelMap);
    		System.out.println("/product/addproduct no session");
    		return modelView;
    	}	
    }
    
    
    @RequestMapping("/product/modifyproduct") 
    @ResponseBody
    public ModelAndView modifyproduct(@RequestBody Product product
    		,HttpServletRequest request){
    	Map<String,Object> modelMap=new HashMap<String,Object>();
//      modelMap=userService.validateUser(username, password, request); 
    	ModelAndView modelView=new ModelAndView();
    	HttpSession session=request.getSession();
    	String username=(String)session.getAttribute("username");
    	if(username!=null){
    		product.setSeller_name(username);
        	modelMap=productService.modifyProductInfo(product);
        	modelView.addAllObjects(modelMap);
        	System.out.println("/product/modifyproduct");
        	return modelView;
    	}else{
    		modelMap.put("result", "fail");
    		modelView.addAllObjects(modelMap);
    		System.out.println("/product/modifyproduct no session");
    		return modelView;
    	}
    	
    }
    
    @RequestMapping("/product/buyproduct") 
    @ResponseBody
    public ModelAndView buyproduct(@RequestBody Order order,HttpServletRequest request){  
    	Map<String,String> modelMap=new HashMap<String,String>();
    	
    	ModelAndView modelView=new ModelAndView();
    	HttpSession session=request.getSession();
    	String username=(String)session.getAttribute("username");
    	System.out.println(username);
    	order.setBuyer_name(username);
    	System.out.println("增加订单"+username);
    	order.setAddress(productService.getProductDao().selectaddressByUsername(username));
    	if(username!=null){
    		productService.Buyproduct(order.getProduct_id(), order.getProduct_number());
    		int price=productService.getProductDao().findProductInfoById(order.getProduct_id()).getProduct_price();
    		order.setTotalPrice(price*order.getProduct_number());
    		orderService.addOrder(order);
        	modelMap.put("result", "success");
    		modelView.addAllObjects(modelMap);
        	  
        	System.out.println("/product/buyproduct");
        	return modelView;
    	}else{
    		modelMap.put("result", "fail");
    		modelView.addAllObjects(modelMap);
    		System.out.println("/product/buyproduct no session");
    		return modelView;
    	}
    	
    	
    }
    
    @RequestMapping("/product/showassess") 
    @ResponseBody
    public ModelAndView showassess(@RequestBody Product product,HttpServletRequest request){  
    	Map<String,Object> modelMap=new HashMap<String,Object>();
//      modelMap=userService.validateUser(username, password, request);  
    	
    	ModelAndView modelView=new ModelAndView();
    	HttpSession session=request.getSession();
    	String username=(String)session.getAttribute("username");
    	if(username!=null){
    		modelMap=productService.getProductAssess(product.getProduct_id());
        	modelView.addAllObjects(modelMap);  
        	System.out.println("/product/showassess");
        	return modelView;
    	}else{
    		modelMap.put("result", "fail");
    		modelView.addAllObjects(modelMap);
    		System.out.println("/product/showassess no session");
    		return modelView;
    	}
    	
    }
}
